package ink.ykb.demo.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ink.ykb.demo.config.CodeConstant;
import ink.ykb.demo.config.MessageUtil;
import ink.ykb.demo.config.ResponseMsg;
import ink.ykb.demo.permission.annotation.Function;
import ink.ykb.demo.permission.entity.Rule;
import ink.ykb.demo.permission.service.RuleService;

/**
 * 权限相关服务
 * 
 * @author mading
 *
 */
@RestController
@RequestMapping("/rule")
public class RuleController {

  @Autowired
  private RuleService ruleService;
  @Autowired
  private MessageUtil messageUtil;

  /**
   * 添加权限
   * 
   * @param rule
   */
  @PostMapping(value = "/add")
  @Function("admin.rule.add")
  public ResponseMsg<String> add(Rule rule) {
    ruleService.add(rule);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 更新权限
   * 
   * @param rule
   */
  @Function("admin.rule.update")
  @PostMapping(value = "/update")
  public ResponseMsg<String> update(Rule rule) {
    ruleService.update(rule);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 删除权限
   * 
   * @param ruleId
   */
  @Function("admin.rule.delete")
  @PostMapping(value = "/delete")
  public ResponseMsg<String> delete(@RequestParam("ruleId") Long ruleId) {
    ruleService.delete(ruleId);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 查询权限列表
   * 
   * @return
   */
  @Function("admin.rule.list")
  @GetMapping(value = "/list")
  public ResponseMsg<List<Rule>> getRuleList() {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        ruleService.getRuleList());
  }
  
  /**
   * 获取权限信息
   * 
   * @param ruleId
   * @return
   */
  @Function("admin.rule.get")
  @GetMapping(value = "/get")
  public ResponseMsg<Rule> getRule(@RequestParam("ruleId") Long ruleId) {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        ruleService.getRule(ruleId));
  }

}
